﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Ext.Net;
using ECommerce.Library.DataStructure;
using AutoGear.BLL;
using ECommerce.Library.Web;
using Entity;

public partial class myaccount_address_list : SitePublicBasePage
{
    protected void Page_Load(object sender, EventArgs e)
    {
        uccResponseMessage.Visible = false;
        
       

        if (!IsPostBack)
        {
            if (!SessionManager.IsUserLoggedIn)
            {
                Response.Redirect(string.IsNullOrEmpty(SupplierId) ? CustomSiteMap.Login : "~/partner/" + SupplierId + "/login.aspx");
            }

            SetPageTitleAndBreadcramb();
        }
    }


    private void SetPageTitleAndBreadcramb()
    {
        ContentPlaceHolder ph = (ContentPlaceHolder)this.Master.Master.FindControl("cphMiddleContent");

        Literal litPageTitle = (Literal)ph.FindControl("litPageTitle");
        Literal litCurrentPage = (Literal)ph.FindControl("litCurrentPage");

        if (litPageTitle != null)
        {
            litCurrentPage.Text = litPageTitle.Text = "Address list";

        }
    }

    protected void Store1_RefreshData(object sender, StoreRefreshDataEventArgs e)
    {
        BindAddress(e.Sort + (e.Dir.ToString() == "Default" ? "" : " " + e.Dir), e.Start, e.Limit);
    }
    private void BindAddress(string sort, int start, int limit)
    {
       
            ReadWriteCustomCollection<AddressBook> data = null;
            int totalCount = 0;
            string sqlCondition = string.Empty;
            sqlCondition += " UserId ='" + SessionManager.LoggedUser.UserId + "'";
            start = start * limit;

            data = AddressBook.GetBackendAddressBookListByCondition(sqlCondition, sort, start, limit);
            totalCount = AddressBook.GetBackendAddressListByConditionCount(sqlCondition);

            Store1.DataSource = data;
            Store1.DataBind();

            (Store1.Proxy[0] as PageProxy).Total = totalCount;
    }


    [DirectMethod(Namespace = "Polana.WebService")]
    public void UpdateAddressBook(int id, string userFirstName, string userLastName, string userTelephone1, string userAddressLine1, string userAddressLine2, string companyName, string userEmailAddress, bool isDefault)
    {
        AddressBook address = AddressBook.Load(id);
        address.UserFirstName = userFirstName;
        address.UserLastName = userLastName;
        address.UserTelephone1 = userTelephone1;
        address.UserAddressLine1 = userAddressLine1;
        address.UserAddressLine2 = userAddressLine2;
        address.CompanyName = companyName;
        address.UserEmailAddress = userEmailAddress;
        address.IsDefault = isDefault;
        address.Update();
        Store1.DataBind();
    }

    [DirectMethod(Namespace = "Polana.WebService")]
    public void DeleteAddress(string ids)
    {
        ids = ids.TrimStart(' ');
        string[] idArray = ids.Split(' ');
        List<int> idList = new List<int>();
        foreach (string id in idArray) idList.Add(Convert.ToInt32(id));
        AddressBook.DeleteAddress(idList);
        Store1.DataBind();
    }

    
}